<?php

namespace app\client\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\learn\LearnSayAntiForgetting as LearnSayAntiForgettingModel;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\say\SayTeachSentence;
use app\common\model\banxi\student\Student;
use app\common\service\learn\LearnSkRecordService;

/*
 * 全科抗遗忘
 * */

class LearnSayAntiForgetting extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 查询-我的抗遗忘记录
     *
     * @return \think\response\Json
     */
    public function listMyListToday()
    {
        $params = $this->request->get(['index', 'limit', 'status']);
        // 获取当前用户ID
        $studentId = $this->auth->id;
        $where = [
            ['studentId', '=', $studentId],
            ['date', '=', date('Y-m-d')]
        ];
        $status = $params['status'] ?? '';
        if (in_array($status, [1, 2])) {
            if ($status == 1) {
                $where[] = ['status', '<=', $status];
            } else {
                $where[] = ['status', '=', $status];
            }
        }
        $pageParams = get_page_params($params);
        $LearnSubjectAntiForgettingModel = new LearnSayAntiForgettingModel();
        $forgettingList = $LearnSubjectAntiForgettingModel
            ->with([
                'trainerInfo' => function ($query) {
                    $query->field('id,name');
                }])
            ->where($where)
            ->order(['status' => 'asc', 'date' => 'desc', 'time' => 'asc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit']
            ]);


        $this->ok('操作成功', result_row($forgettingList));
    }

    public function listMyListBefore()
    {
        $params = $this->request->get(['index', 'limit', 'status']);
        // 获取当前用户ID
        $studentId = $this->auth->id;
        $where = [
            ['studentId', '=', $studentId],
            ['date', '<', date('Y-m-d')]
        ];
        $status = $params['status'] ?? '';
        if (in_array($status, [1, 2])) {
            if ($status == 1) {
                $where[] = ['status', '<=', $status];
            } else {
                $where[] = ['status', '=', $status];
            }
        }
        $pageParams = get_page_params($params);
        $LearnSubjectAntiForgettingModel = new LearnSayAntiForgettingModel();
        $forgettingList = $LearnSubjectAntiForgettingModel
            ->with([
                'trainerInfo' => function ($query) {
                    $query->field('id,name');
                }])
            ->where($where) // 日期小于等于今天
            ->order(['status' => 'asc', 'date' => 'desc', 'time' => 'asc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit']
            ]);


        $this->ok('操作成功', result_row($forgettingList));
    }

    /*
     * 开始抗遗忘
     * */
    public function startAntiForgetting()
    {
        $id = $this->request->post('id', 0);

        $studentId = $this->auth->id;

        $LearnSkRecordService = new LearnSkRecordService();
        $info = $LearnSkRecordService->startSayForgetting($id, $studentId,AccountTypeEnum::STUDENT);
        $this->ok('操作成功', $info);
    }

    public function listForgettingData()
    {
        $forgettingId = $this->request->get('forgettingId');
        if (empty($forgettingId)) {
            $this->fail('请选择要查看的记录');
        }
        $LearnSayAntiForgettingModel = new LearnSayAntiForgettingModel();
        $reviewData = $LearnSayAntiForgettingModel->where('id', $forgettingId)->find();
        if (empty($reviewData)) {
            $this->fail('数据不存在');
        }
        $sayTeachSentenceModel = new SayTeachSentence();
        $forgettingAnswerList = $sayTeachSentenceModel->alias('a')
            ->join('banxi_learn_say_anti_forgetting_answer b', 'b.sayTeachSentenceId = a.id')
            ->where([
                'b.forgettingId' => $forgettingId,
                'b.del' => 0
            ])->field('b.id,a.sentenceEn,a.sentenceZh,a.sentencePhonetic,a.sentenceAudio,a.sentenceImage,a.sentenceWordColor,a.question,a.answer,b.isCorrect,b.listenCorrect,b.translationCorrect,b.questionCorrect')->select();
        if (empty($forgettingAnswerList)) {
            $this->fail('数据不存在');
        }
        foreach ($forgettingAnswerList as $item) {
            $item['questionList'] = $sayTeachSentenceModel->splitQuestion($item['question'], $item['answer']);
        }
        $this->ok('ok', $forgettingAnswerList);
    }

    /*
     * 结束抗遗忘
     * */
    public function endAntiForgetting()
    {
        $forgettingId = $this->request->post('forgettingId', 0);
        $answerList = $this->request->post('answerList', null, null);
        if (!$answerList) {
            $this->fail('请选择掌握状态');
        }
        $studentId = $this->auth->id;
        $LearnSkRecordService = new LearnSkRecordService();
        $LearnSkRecordService->answerSayForgetting($forgettingId, $studentId,AccountTypeEnum::STUDENT, $answerList);
        $this->ok();
    }

    /*
     * 查询-抗遗忘报告
     * */
    public function getForgettingReport()
    {
        $forgettingId = $this->request->get('forgettingId');
        $LearnSubjectAntiForgettingModel = new LearnSayAntiForgettingModel();
        $record = $LearnSubjectAntiForgettingModel
            ->where([
                'id' => $forgettingId
            ])
            ->find();
        if (!$record) {
            $this->fail('抗遗忘记录不存在');
        }

        $studentInfo = null;
        if ($record['studentId']) {
            $studentInfo = Student::with([
                'gradeInfo' => function ($query) {
                    $query->field('id,name');
                },
                'organizationBaseInfo' => function ($query) {
                    $query->field('id,name,managerName');
                }
            ])->where(['id' => $record['studentId']])->field('id,avatar,name,organizationId,englishLevel')->find();
            if ($studentInfo['organizationId'] == 0) {
                $organizationModel = new Organization();
                $studentInfo['organizationBaseInfo'] = $organizationModel->getOrganizationInfo();
            }
        }
        $record['student'] = $studentInfo;
        $sayTeachSentenceModel = new SayTeachSentence();
        $list = $sayTeachSentenceModel->alias('a')
            ->join('banxi_learn_say_anti_forgetting_answer b', 'b.sayTeachSentenceId = a.id')
            ->where([
                ['b.forgettingId', '=', $forgettingId],
                ['b.del', '=', 0],
                ['b.isCorrect', '=', 0]
            ])->field('b.id,a.sentenceEn,a.sentenceZh,a.sentenceAudio,b.isCorrect,b.listenCorrect,b.translationCorrect,b.questionCorrect')->select();

        $this->ok('操作成功', ['forgettingRecord' => $record, 'list' => $list]);
    }
}